package com.hey.amqpdemo.producer;

import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * @ClassName PayProducer
 * @Author hey
 * @Date 2019/3/25 13:18
 **/
@Component
public class PayProducer {
    @Autowired
    RabbitTemplate rabbitTemplate;


    public void send() {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        //声明消息处理器
        MessagePostProcessor messagePostProcessor = message -> {
            MessageProperties messageProperties = message.getMessageProperties();
            messageProperties.setContentEncoding("utf-8");

//            messageProperties.setExpiration("5"); //设置消息过期时间,到这个时间消息就过期，不管消费了没有，也不管确认了没有
            return message;
        };
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("pay1Exchange","pay1.mine","这个是消息："+i,messagePostProcessor,correlationData);
        }

    }
}
